/********************************************/
  // Creation History
  // --------------------------------------------------------------
  //	Company:			HTHK
  //	NAME:				ProjectGroupServlet.java
  //    Title   :			operate A ProjectGroup Record such as:Create,Update,Delete
  //	FUNCTION:
  //	BY:				Poly Li
  //	DATE:				2004-01-09
  // --------------------------------------------------------------
/*********************************************/
// Copyright (c) 2004 HTHK

package tss.projectgroup;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.sql.*;

import com.ffpc.sql.*;
import com.ffpc.struts.StrutsMessage;

import tss.common.DBServlet2;


public class ProjectGroupServlet
  extends DBServlet2 {

  @Override
protected String perform(  HttpServletRequest request,
                             HttpServletResponse response,
                             StrutsMessage message)
    throws  ServletException,
            IOException,
            SQLException,
            DataException,
            Exception
  {
     //New A Form Action
     String action = new String(request.getParameter("formAction"));
     //New A ProjectGroup Record
     ProjectGroupBean projectgroup = ProjectGroupProcess.createBean();

     //Get A ProjectGroup Record
     projectgroup.setProject_name(request.getParameter("project_name"));
     projectgroup.setGroup_name(request.getParameter("group_name"));

     //Deal different operations
     //Insert
      if (action.trim().equalsIgnoreCase("Create"))
      {
        //Set ProjectGroup's Create Date and Create By
        //projectgroup.setCreate_date(DateFormatter.format(this.getDate()));
        //projectgroup.setCreate_by(getUser().getUser_name());

        ProjectGroupBean existProjectGroup = ProjectGroupProcess.createBean();
        existProjectGroup.setProject_name(request.getParameter("project_name"));
        ProjectGroupBean[] projectGroups = ProjectGroupProcess.find(this.getConnection(),existProjectGroup);

        if (projectGroups==null || projectGroups.length==0 )
        {
	        if (ProjectGroupProcess.insert(this.getConnection(),projectgroup)>0)
	            message.setMessage("Save successfully.");
	        else
	            message.setMessage("Save failure");
	}
	else
		message.setMessage("Sorry,project "+projectGroups[0].getProject_name()+"  had already exist in group "+projectGroups[0].getGroup_name()+" !");
      }
      //Update
      else if (action.trim().equalsIgnoreCase("Update"))
      {
        //Set ProjectGroup's Update Date and Update By
        //projectgroup.setUpd_date(DateFormatter.format(this.getDate()));
        //projectgroup.setUpd_by(getUser().getUser_name());
        if (ProjectGroupProcess.update(this.getConnection(), projectgroup) > 0)
          message.setMessage("Save successfully.");
        else
          message.setMessage("Save failure.");
      }
      //Delete
      else if (action.trim().equalsIgnoreCase("Delete"))
        if (ProjectGroupProcess.delete(this.getConnection(),projectgroup)>0)
            message.setMessage("Delete successfully.");
        else
            message.setMessage("Delete failure.");
      else
            message.setMessage("No action mapped!");

      return "tss/projectgroup/ProjectGroupSearch.jsp";
  }
}


